package com.smartboard.network.ui;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.smartboard.network.R;
import com.smartboard.network.client.GameClient;
import com.smartboard.network.client.GameEventListenerBase;
import com.smartboard.network.game.AbstractGame;
import com.smartboard.network.game.IRoom;
import com.smartboard.network.game.IUser;

import java.util.List;

public class RoomActivity extends AppCompatActivity {
    private MyGameEventListener mGameEventListener;
    private ListView mRoomListView;
    private AppCompatDialog mCreateRoomDialog;
    private AppCompatDialog mEnterRoomDialog;

    private MyAdapter mAdapter;
    private List<IRoom> mRooms;
    private IRoom mEnterRoom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.network_activity_room);
        setTitle(R.string.title_room_list);

        mGameEventListener = new MyGameEventListener();

        mRoomListView = (ListView)findViewById(R.id.list);
        mAdapter = new MyAdapter();

        FloatingActionButton addButton = (FloatingActionButton) findViewById(R.id.add);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCreateRoomDialog();
            }
        });

        mRoomListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mEnterRoom = (IRoom)mAdapter.getItem(position);
                if(mEnterRoom.needPassword()) {
                    showEnterRoomDialog();
                } else {
                    GameClient.inst().enterRoom(mEnterRoom, null);
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mRooms = GameClient.inst().getRooms();
        GameClient.inst().registerEventListener(mGameEventListener);
        mRoomListView.setAdapter(mAdapter);
    }

    @Override
    public void onPause() {
        super.onPause();
        GameClient.inst().unregisterEventListener(mGameEventListener);
    }

    private void showCreateRoomDialog() {
        if(mCreateRoomDialog != null && mCreateRoomDialog.isShowing())
            return;
        if(mCreateRoomDialog == null)
            mCreateRoomDialog = new CreateRoomDialog(this);
        mCreateRoomDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mCreateRoomDialog = null;
            }
        });
        mCreateRoomDialog.show();
    }

    private void showEnterRoomDialog() {
        if(mEnterRoomDialog != null && mEnterRoomDialog.isShowing())
            return;
        if(mEnterRoomDialog == null)
            mEnterRoomDialog = new EnterRoomPasswordDialog(this, mEnterRoom);
        mEnterRoomDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mEnterRoomDialog = null;
            }
        });
        mEnterRoomDialog.show();
    }

    private class MyGameEventListener extends GameEventListenerBase {

        @Override
        public void onRoomNumChanged(List<IRoom> rooms) {
            mRooms = GameClient.inst().getRooms();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mAdapter.notifyDataSetChanged();
                }
            });
        }

        @Override
        public void onRoomUserChanged(IRoom room, IUser user, boolean leave) {
            // is me enter room?
            if(!leave && room.equals(mEnterRoom) && user.equals(GameClient.inst().getUser())) {
                GameActivity.enter(RoomActivity.this, mEnterRoom);
                if(mEnterRoomDialog != null)
                    mEnterRoomDialog.dismiss();
            }
        }

        @Override
        public void onRespError(int cmd, int errorCode) {
            if(cmd == AbstractGame.CMD_ENTER_ROOM && errorCode == AbstractGame.ERROR_INVALID_PASSWORD) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RoomActivity.this, R.string.error_incorrect_password, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mRooms.size();
        }

        @Override
        public Object getItem(int i) {
            return mRooms.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(RoomActivity.this).inflate(
                        R.layout.network_room_list_item, null, false);
            }

            IRoom room = (IRoom)getItem(position);
            TextView nameTextView = (TextView)convertView.findViewById(R.id.name);
            nameTextView.setText(room.getName() + getString(R.string.user_count, room.getUsers().size()));
            return convertView;
        }
    }

    public static void enter(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, RoomActivity.class);
        activity.startActivity(intent);
    }
}
